package com.ndp.fb.fbagent.service.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.ndp.fb.fbagent.service.impl.paramadapter.FacebookParamAdapter;
import com.ndp.fb.fbagent.service.webrequest.AccountQueueWebRequestor;
import com.ndp.fb.model.fbagent.param.APIPriority;
import com.ndp.fb.model.fbagent.param.FacebookParam;
import com.ndp.fb.model.fbagent.param.advideo.AdVideoParam;
import com.restfb.*;
import org.apache.commons.lang3.SerializationUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * Created by Mikey.zhaopeng on 2015/3/23 17:28.
 * Email mikey.zhao@ndpmedia.com
 */
public class AdVideoFBServiceImplTester {
    public static final Version VERSION = Version.Version_2_6;
    private final Logger logger = LoggerFactory.getLogger(getClass());
    FacebookParamAdapter facebookParamAdapter = new FacebookParamAdapter();
    AccountQueueWebRequestor accountQueueWebRequestor = new AccountQueueWebRequestor();

    @Test
    public void updateAdVideoTest() {
        String result = null;
        try {
            String accountId = "1390685454498661";
            String token = "CAAVRVmfKslwBAEZB1sX8tzf2iDAfaKxcnN0u4joi4qweHTugQupaE0UaZCtL21ccOwvZC8NBJkfcN92GzvUNWbZAyrM01SNAGUYBlQQNLidHLmSQJ1U9CZCCTwqZCg8SgAgZAnpToiE6OsKLm3ytGuBSaPZBahcFxRMDeTEHu7U4pZCsZBEtr6vRiHgifzrZCveEGWdpkniHroT3ZAoS8gXQY09L";
            Map<String, String> equipment = new HashMap<>();
            equipment.put("entrustAccountId", accountId);
            equipment.put("accessToken", token);
            File file = new File("C:\\Users\\imzhp\\Desktop\\test.mp4");
            byte[] fileByes = SerializationUtils.serialize(file);

            AdVideoParam requestParam = new AdVideoParam();
//            requestParam.setFileBytes(fileByes);
            requestParam.setApiPriorityCode(APIPriority.UploadImage.getCode());
            requestParam.setEquipment(equipment);

            requestParam.setFbAccountId(Long.parseLong(accountId));

            Map map = this.adapterMap(requestParam);
            List<BinaryAttachment> binaryAttachments = (List<BinaryAttachment>) map.get("binaryAttachments");
            Parameter[] parameters = (Parameter[]) map.get("parameters");

            String uri = "/act_1390685454498661/advideos";
            result = this.makePostRequest(uri, token, binaryAttachments, parameters);
            System.out.println(result);
        } catch (NumberFormatException e) {
            logger.error(e.getMessage(), e);
        }
    }

    protected DefaultFacebookClient getFacebookClient(String accessToken) {
        return new DefaultFacebookClient(accessToken, accountQueueWebRequestor, new DefaultJsonMapper(), VERSION);
    }

    public String makePostRequest(String endpoint, String accessToken, List<BinaryAttachment> binaryAttachments, Parameter[] parameters) {
        DefaultFacebookClient facebookClient = getFacebookClient(accessToken);
        return facebookClient.makeRequest(endpoint, true, false, binaryAttachments, parameters);
    }

    protected Map adapterMap(FacebookParam param) {
        Map map = new HashMap();
        List params = this.adapter(param);
        if (CollectionUtils.isNotEmpty(params)) {
            List parameters = new ArrayList();
            List binaryAttachments = new ArrayList();
            for (Object object : params) {
                if (object instanceof Parameter) {
                    parameters.add(object);
                } else if (object instanceof BinaryAttachment) {
                    binaryAttachments.add(object);
                }
            }
            map.put("parameters", parameters.toArray(new Parameter[parameters.size()]));
            map.put("binaryAttachments", binaryAttachments);
        }
        return map;
    }

    protected List<Parameter> adapter(FacebookParam param) {
        return facebookParamAdapter.adapterParam(param);
    }

    @Test
    public void testUpload() {
        try {
            Properties prop = System.getProperties();
//            prop.setProperty("https.proxyHost", "proxy.dy");
//            prop.setProperty("https.proxyPort", "3128");
//            prop.setProperty("https.proxyHost", "127.0.0.1");
//            prop.setProperty("https.proxyPort", "1080");


            String token = "CAAVRVmfKslwBABZBLV9P4csyILg1unY4falTCaBbIOUnVvoBYs9EQjZB2O4jPuD0UrZAJZBmPXwxlnjorTdm1iBILqeQufXVuBNNTA07w77OlE6VUoAH6c4HNOy3MoojFImoNTnZCQqG5UsINB2cOEzjQt0X30BNPCZARim54KuZCy9xVWzWpoA0YYjEsYeY6NMo0kwEnsugyp96UzlQn6f";
            DefaultFacebookClient defaultFacebookClient = new DefaultFacebookClient(token, new DefaultWebRequestor(),
                    new DefaultJsonMapper(), Version.Version_2_2);
            InputStream input = new FileInputStream("C:\\Users\\Administrator\\Desktop\\4M.mp4");
            byte[] byt = new byte[input.available()];
            input.read(byt);
            BinaryAttachment body = BinaryAttachment.with("source", byt);
            String publish = defaultFacebookClient.publish("act_1390685454498661/advideos", String.class, body);
            System.out.println(publish);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }


    @Test
    public void testUpload1() {
        try {
//            Properties prop = System.getProperties();
//            prop.setProperty("https.proxyHost", "proxy.dy");
//            prop.setProperty("https.proxyPort", "3128");
//            String token = "CAAVRVmfKslwBABZBLV9P4csyILg1unY4falTCaBbIOUnVvoBYs9EQjZB2O4jPuD0UrZAJZBmPXwxlnjorTdm1iBILqeQufXVuBNNTA07w77OlE6VUoAH6c4HNOy3MoojFImoNTnZCQqG5UsINB2cOEzjQt0X30BNPCZARim54KuZCy9xVWzWpoA0YYjEsYeY6NMo0kwEnsugyp96UzlQn6f";
//            DefaultFacebookClient defaultFacebookClient = new DefaultFacebookClient(token, new DefaultWebRequestor(),
//                    new DefaultJsonMapper(), Version.Version_2_6);
//            File file = new File("C:\\Users\\Administrator\\Desktop\\4M.mp4");
//            BinaryAttachment body = BinaryAttachment.with("source", FileUtils.readFileToByteArray(file));
//
//            InputStream input = new FileInputStream("C:\\Users\\Administrator\\Desktop\\4M.mp4");
//            byte[] byt = new byte[input.available()];
//            input.read(byt);
//            BinaryAttachment binaryAttachment = BinaryAttachment.with("4M.mp4", input);
//
//            String publish = defaultFacebookClient.publish("act_1390685454498661/advideos", String.class, binaryAttachment);
//
//            System.out.println(publish);


            String token = "CAAVRVmfKslwBABZBLV9P4csyILg1unY4falTCaBbIOUnVvoBYs9EQjZB2O4jPuD0UrZAJZBmPXwxlnjorTdm1iBILqeQufXVuBNNTA07w77OlE6VUoAH6c4HNOy3MoojFImoNTnZCQqG5UsINB2cOEzjQt0X30BNPCZARim54KuZCy9xVWzWpoA0YYjEsYeY6NMo0kwEnsugyp96UzlQn6f";
            DefaultFacebookClient defaultFacebookClient = new DefaultFacebookClient(token, new DefaultWebRequestor(), new DefaultJsonMapper(), Version.Version_2_2);
//            InputStream inputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\4M.mp4");
//            BinaryAttachment binaryAttachment = BinaryAttachment.with("4M.mp4", inputStream);


            InputStream input = new FileInputStream("C:\\Users\\Administrator\\Desktop\\4M.mp4");
            byte[] byt = new byte[input.available()];
            input.read(byt);
            BinaryAttachment binaryAttachment = BinaryAttachment.with("4M.mp4", input);



            Object result = defaultFacebookClient.publish("act_1390685454498661/advideos", String.class, binaryAttachment, Parameter.with("message", "Test cat"));
            System.out.println(result);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }


}
